using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Linq.Extensions;
using Abp.Extensions;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using SOEI.Solution.Application;
using SOEI.Solution;
using SOEI.Solution.FileUpLoader;
using SOEI.Solution.FileUpLoader.DomainService;
using SOEI.Solution.FileUpLoader.Dtos;


using SOEI.Solution.Authorization;

namespace SOEI.Solution.FileUpLoader
{
	/// <summary>
	/// 【扩展模块】  <br/>
	/// 【功能描述】  ：XXX 模块<br/>
	/// 【创建日期】  ：2020.05.21 <br/>
	/// 【开发人员】  ：static残影<br/>
	///</summary>
	[ApiExplorerSettings(GroupName = "Manager", IgnoreApi = false)]
	public class AttachAppService : SolutionAppServiceBase, IAttachAppService
	{
		/// <summary>
		///【Attach】仓储层
		/// </summary>
		private readonly IRepository<Attach, long>	_attachRepository;

		/// <summary>
		///【Attach】领域服务
		/// </summary>
		private readonly IAttachManager _attachManager;
		
		public AttachAppService(
			IRepository<Attach, long>  attachRepository,
			IAttachManager attachManager
		)
		{
			_attachRepository = attachRepository;
			_attachManager=attachManager;
		}
		
		#region -------------------------------------------------辅助工具生成---------------------------------------------- 
		
		/// <summary>
		///【Attach】获取的分页列表信息
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		
		public async Task<PagedResultDto<AttachListDto>> GetPaged(GetAttachsInput input)
		{
			var query = _attachRepository.GetAll()
							//模糊搜索 字段FileName
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.FileName.Contains(input.FilterText))                                                                                      
							//模糊搜索 字段Path
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Path.Contains(input.FilterText))                                                                                      
							//模糊搜索 字段Extension
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.Extension.Contains(input.FilterText))                                                                                      
							//模糊搜索 字段ContentType
						  .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.ContentType.Contains(input.FilterText))                                                                                      
			;
			// TODO:根据传入的参数添加过滤条件

			var count = await query.CountAsync();

			var entityList = await query
					.OrderBy(input.Sorting).AsNoTracking()
					.PageBy(input)
					.ToListAsync();

			var entityListDtos = ObjectMapper.Map<List<AttachListDto>>(entityList);

			return new PagedResultDto<AttachListDto>(count,entityListDtos);
		}

		/// <summary>
		///【Attach】通过指定id获取MemberListDto信息
		/// </summary>
		
		public async Task<AttachListDto> GetById(EntityDto<long> input)
		{
			var entity = await _attachRepository.GetAsync(input.Id);

			var dto = ObjectMapper.Map<AttachListDto>(entity);
			return dto;
		}

		/// <summary>
		///【Attach】 获取编辑
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		
		public async Task<GetAttachForEditOutput> GetForEdit(NullableIdDto<long> input)
		{
			var output = new GetAttachForEditOutput();
			AttachEditDto editDto;

			if (input.Id.HasValue)
			{
				var entity = await _attachRepository.GetAsync(input.Id.Value);
				editDto = ObjectMapper.Map<AttachEditDto>(entity);
			}
			else
			{
				editDto = new AttachEditDto();
				editDto.ModuleID = Guid.NewGuid();
			}
			output.Attach = editDto;
			return output;
		}
		/// <summary>
		///【Attach】 添加或者修改的公共方法
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		
		public async Task CreateOrUpdate(CreateOrUpdateAttachInput input)
		{
			
			if (input.Attach.Id.HasValue)
			{
				await Update(input.Attach);
			}
			else
			{
				await Create(input.Attach);
			}
		}
		/// <summary>
		///【Attach】新增
		/// </summary>
		
		protected virtual async Task<AttachEditDto> Create(AttachEditDto input)
		{
			//TODO:新增前的逻辑判断，是否允许新增

			var entity = ObjectMapper.Map<Attach>(input);
			//调用领域服务
			entity = await _attachManager.CreateAsync(entity);

			var dto=ObjectMapper.Map<AttachEditDto>(entity);
			return dto;
		}

		/// <summary>
		///【Attach】编辑
		/// </summary>
		
		protected virtual async Task Update(AttachEditDto input)
		{
			//TODO:更新前的逻辑判断，是否允许更新
			var key =  input.Id.Value;
			var entity = await _attachRepository.GetAsync(key);
			//  input.MapTo(entity);
			//将input属性的值赋值到entity中
			ObjectMapper.Map(input, entity);
			await _attachManager.UpdateAsync(entity);
		}

		/// <summary>
		///【Attach】删除信息
		/// </summary>
		/// <param name="input"></param>
		/// <returns></returns>
		
		public async Task Delete(EntityDto<long> input)
		{
			//TODO:删除前的逻辑判断，是否允许删除
			await _attachManager.DeleteAsync(input.Id);
		}

		/// <summary>
		///【Attach】 批量删除Member的方法
		/// </summary>
		
		public async Task BatchDelete(List<long> input)
		{
			// TODO:批量删除前的逻辑判断，是否允许删除
			await _attachManager.BatchDelete(input);
		}

		#endregion

		#region -------------------------------------------------用户自定义------------------------------------------------
		/*请在此扩展应用服务实现*/

		

		#endregion
	}
}
